<?php
namespace app\ index\ controller;
use app\ common\ controller\ Base;
use think\ facade\ Request;
use app\ common\ model\ User as UserModel;
use think\ facade\ Session;
class User extends Base {
    //处理用户表中的一些逻辑
    //注册页面
    public function register() {
        $this->assign( 'title', '用户注册' );
        //将注册用户渲染出来
        return $this->fetch();
        //上面这两个函数都是Controller类中的方法
    }
    
    //处理用户提交的注册信息
    public function insert() {

        if ( Request::isAjax() ) {
            //使用模型来创建数据
            //验证数据
            $data = Request::post(); //要验证的数据
            $rule = 'app\common\validate\User';
            $res = $this->validate( $data, $rule );

            if ( $res !== true ) {
                return [ 'status' => -1, 'message' => $res ];
            } else {
                //获取用户通过提交表单过来的除了确认密码之外的所有数据
                $data = Request::except( 'password_confirm', 'post' );
                if ( $user = UserModel::create( $data ) ) {
                    //注册成功后实现自动登录
                    $res = UserModel::get($user->id);
                    Session::set('user_id',$res->id);
                    Session::set('user_name',$res->name);
                    Session::set('user_level',$res->is_admin);
                    return [ 'status' => 1, 'message' => '注册成功' ];
                } else {
                    return [ 'status' => 0, 'message' => '注册失败' ];
                }
            }


        } else {
            $this->error( "请求类型错误", 'register' );
        }
    }

    //用户登录
    public function login() {
        $this->Logined();
        return $this->view->fetch( 'login', [ 'title' => '用户登录' ] );
    }
    
    //登录验证
    public function loginCheck(){
        if ( Request::isAjax() ) {
            //使用模型来创建数据
            //验证数据
            $data = Request::post(); //要验证的数据
            $rule = [
                'email|邮箱'=>'require|email',
                'password|密码'=>'require|alphaNum',
            ];//自定义的验证规则
            //开始验证
            $res = $this->validate( $data, $rule );

            if ( $res !== true ) {
                return [ 'status' => -1, 'message' => $res ];
            } else {//true
                //执行查询
                $result = UserModel::get(function($query) use ($data) {
                   $query->where('email',$data['email']) 
                       ->where('password',sha1($data['password']));
                });
                if ( $result !==null) {
                    //将用户的数据写到session中
                    Session::set('user_id',$result->id);
                    Session::set('user_name',$result->name);
                    Session::set('user_level',$result->is_admin);
                    return [ 'status' => 1, 'message' => '登录成功' ];
                } else {
                    return [ 'status' => 0, 'message' => '邮箱或密码不对' ];
                }
            }


        } else {
            $this->error( "请求类型错误", 'login' );
        }
    }
    
    //退出登录
    public function logout(){
        Session::delete('user_id');
        Session::delete('user_name');
        //全部session删除
        #Session::clear();
        $this->success('成功退出登录','Index/index');
    }
    
}









